/*
 * Copyright © 2010 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.classes;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author jcccn
 * @site http://www.senseforce.com/
 */
public class TestCanvas extends Canvas {
    private Image image1;
    private int[] maskArray1;
    private int[] maskArray2;
    private int[] maskArray3;
    private int imgWidth;
    private int imgHeight;
    private int x;
    private int y;
    private int maskHeight = 0;
    String tickerStr = "";

    public TestCanvas() {
         try {
            image1 = Image.createImage("/taichi.png");
             System.out.println("creating image succeeded.");
        } catch(IOException e) {
            System.out.println("creating image failed.");
        }
        imgWidth = image1.getWidth();
        imgHeight = image1.getHeight();
        maskHeight = imgHeight / 3;
        maskArray1 = new int[imgWidth*maskHeight];
        maskArray2 = new int[imgWidth*maskHeight];
        maskArray3 = new int[imgWidth*maskHeight];
        for (int i = 0; i < imgWidth*maskHeight; i++) {
            maskArray1[i] = 0xff00FF00;
            maskArray2[i] = 0x7f00FF00;
            maskArray3[i] = 0x0000FF00;
        }
        x = (getWidth() - imgWidth) / 2;
        y = 150;

        System.out.println("Pointer Motion "+(this.hasPointerMotionEvents()?"":"not")+"supported!");
    }

    protected void paint(Graphics g) {
        setFullScreenMode(true);
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0, 255, 0);
        g.fillRect(20, 30, 30, 30);
        g.setColor(255, 255, 0);
        g.fillArc(60, 60, 60, 60, 30, 150);
        
        g.setColor(0x000000);
        String str = "Sina.com is the largest web portal.";
        Font font = Font.getFont(Font.FACE_SYSTEM);
        g.setFont(font);
        g.drawSubstring("I love Taobao!", 2, 4, 10, 110, Graphics.LEFT|Graphics.TOP);
        if (font.stringWidth(str) > getWidth()) {
            int cWidth = font.substringWidth(str, 0, 1);
            int numInLine = getWidth()/cWidth;
            g.drawSubstring(str, 0, numInLine, 0, 130, Graphics.LEFT|Graphics.TOP);
            g.drawSubstring(str, numInLine, str.length()-numInLine, 0, 130+font.getHeight(), Graphics.LEFT|Graphics.TOP);
        }
        else {
            g.drawString(str, 0, font.getHeight(), Graphics.LEFT|Graphics.TOP);
        }
        g.drawSubstring("I love Taobao!", 2, 4, 10, 110, Graphics.LEFT|Graphics.TOP);
        g.drawLine(0, 100, getWidth(), 100);

        g.drawImage(image1, x, y, Graphics.LEFT|Graphics.TOP);
        g.drawRGB(maskArray1, 0, imgWidth, x, y, imgWidth, maskHeight, true);
        g.drawRGB(maskArray2, 0, imgWidth, x, y+maskHeight, imgWidth, maskHeight, true);
        g.drawRGB(maskArray3, 0, imgWidth, x, y+maskHeight, imgWidth, maskHeight, true);

        g.drawString(tickerStr, getWidth()/2, 10, Graphics.HCENTER|Graphics.TOP);
    }

    protected void keyPressed(int keyCode) {
        switch (keyCode) {
            case Canvas.KEY_NUM0:   tickerStr = "\"0\" key pressed.";    break;
            case Canvas.KEY_NUM1:   tickerStr = "\"1\" key pressed.";    break;
            case Canvas.KEY_NUM2:   tickerStr = "\"2\" key pressed.";    break;
            case Canvas.KEY_NUM3:   tickerStr = "\"3\" key pressed.";    break;
            case Canvas.KEY_NUM4:   tickerStr = "\"4\" key pressed.";    break;
            case Canvas.KEY_NUM5:   tickerStr = "\"5\" key pressed.";    break;
            default:    tickerStr = " key pressed.";
        }
        switch (getGameAction(keyCode)) {
            case Canvas.UP:       tickerStr = "(Up)"+tickerStr;   break;
            case Canvas.DOWN:     tickerStr = "(Down)"+tickerStr;   break;
            case Canvas.LEFT:     tickerStr = "(Left)"+tickerStr;   break;
            case Canvas.RIGHT:    tickerStr = "(Right)"+tickerStr;   break;
            case Canvas.FIRE:     tickerStr = "(Fire)";   break;
        }
        System.out.println(tickerStr);
        repaint();
    }
    protected void keyReleased(int keyCode) {
        switch (keyCode) {
            case Canvas.KEY_NUM6:   tickerStr = "\"6\" key released.";    break;
            case Canvas.KEY_NUM7:   tickerStr = "\"7\" key released.";    break;
            case Canvas.KEY_NUM8:   tickerStr = "\"8\" key released.";    break;
            case Canvas.KEY_NUM9:   tickerStr = "\"9\" key released.";    break;
            case Canvas.KEY_STAR:   tickerStr = "\"star\" key released.";    break;
            case Canvas.KEY_POUND:   tickerStr = "\"pound\" key released.";    break;
        }
        repaint();
        serviceRepaints();
    }
    protected void poiterPressed(int x, int y) {

    }

    protected void pointerDragged(int x, int y) {

    }

    protected void pointerReleased(int x, int y) {
        
    }

    protected void showNotify() {
        System.out.println("will show");
    }

    protected void hideNotify() {
        System.out.println("will hide");
    }
}
